﻿using System;
using System.Data;

namespace msBsc.WebUi
{
    public partial class wfSysPromoTradingLog : msBsc.Cls.BPageHt
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindControls();
                txtRelId.Value = GetFormParam("REL_ID");
                //this.ExecJScript("$('#txtRelId').val('" + GetFormParam("REL_ID") + "');");
                //if (GetFormParam("REL_ID") != string.Empty)
                //{
                //    this.ExecJScript("$('#divCondition').hide();");
                //}
                //using (msBsc.AppLogic.DataAlc.CBizShpCustomerInfo oinf = new msBsc.AppLogic.DataAlc.CBizShpCustomerInfo())
                //{
                //    decimal dectm = oinf.GetUserPromotionChargeAmt(this.oUserInfo.Operator);
                //    this.SetContrlValue("spntatallamt", dectm.ToString());
                //}
            }
        }

        private void BindControls()
        {
            if (!IsPostBack)
            {
            }
        }
        //重构基类的回调处理函数，以strAction属性来判断是什么类型的回调，该变量的值对应前台的ACTION参数，如：按钮的名称等
        public override void InitialByParam()
        {
            if (this.gtGridRet != null)
            {
                if (this.gtGridRet.action == "load" || this.gtGridRet.action == "export")//处理GRID的数据原获取和数据导出
                {
                    DataSet ds = GetDisplayData(this.GetParamHtValue("gtGridID"), this.strAction, this.sCurrPageRadom);
                    if (ds != null && ds.Tables.Count > 0)
                        SetReturnDatatable(ds.Tables[0], "SCORE_PROMOTION_ID");//CONSTANT_CATEGORY_ID为表的KEY字段，可填可不填
                } 
            }
            else if (strAction == "btnEdit")//根据传入的回调类型进行处理，这里响应编辑按钮
            {
                SetMode("EDIT");
            }
            else if (strAction == "btnNotSave")//根据传入的回调类型进行处理，这里响应编辑按钮
            {
                SetMode("VIEW");
            }
            else if (strAction == "dobtnGetCash")//根据传入的回调类型进行处理，这里响应编辑按钮
            {
                dobtnGetCash( );
            }
            else if (strAction == "btnConfirmPay")//根据传入的回调类型进行处理，这里响应编辑按钮
            {
                dobtnConfirmPay(this.GetParamHtValue("CHECK_ID"));
                this.ExecJScript("btnDo('mFinder');");
            }
            //else if (strAction == "btnCancel")//根据传入的回调类型进行处理，这里响应编辑按钮
            //{
            //    SetItemStatus(this.GetParamHtValue("CHECK_ID"), "False");
            //    this.ExecJScript("btnDo('mFinder');");
            //}

            base.InitialByParam();
        }

        private void dobtnGetCash()
        {
            decimal decCash = 0M;
            if (!decimal.TryParse(GetParamHtValue("TRADING_AMT"), out decCash))
            {
                return;
            }
            if (decCash <= 0M)
            {
                return;
            }

            using (msBsc.AppLogic.DataAlc.CBizShpPromotionScoreLog objtem1 = new msBsc.AppLogic.DataAlc.CBizShpPromotionScoreLog())
            {
                System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
                transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
                transactionOption.Timeout = new TimeSpan(0, 10, 0);
                using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
                {
                    objtem1.AddTradingLogForGetCash(decCash.ToString(), this.oUserInfo.Operator, this.oUserInfo.Operator, "4");
                    ts.Complete();
                }
                this.ExecJScript("ClosePop('dvItemEdit1');");
            }
        }

        private void dobtnConfirmPay(string strIds )
        {
            using (msBsc.AppLogic.DataAlc.CBizShpPromotionScoreLog olog = new AppLogic.DataAlc.CBizShpPromotionScoreLog())
            {
                System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
                transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
                transactionOption.Timeout = new TimeSpan(0, 10, 0);
                using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
                {
                    DataSet ds = olog.SelectByIds(strIds);
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        if (ds.Tables[0].Rows[i]["TRADING_SID"].ToString() == "待支付")
                        {
                            ds.Tables[0].Rows[i]["TRADING_SID"] = "已支付";
                        }
                    }
                    if (ds.HasChanges())
                    {
                        olog.Update(ds);
                        ds.AcceptChanges();

                    }
                    ts.Complete();
                }
            }
        }

        //修改页面状态
        private void SetMode(string strMode)
        {
            EditMode = "VIEW";
            switch (strMode)
            {
                case "EDIT":
                    //EnabledLinkBtn("btnEdit", false);
                    //EnabledLinkBtn("btnSave", true);
                    //EnabledLinkBtn("btnNotSave", true);
                    //EnabledLinkBtn("btnConfirm", false);
                    //EnabledLinkBtn("btnCancel", false);
                    this.ExecJScript("doEditGt('myGrid1');");
                    break;
                case "ADD":
                    //EnabledLinkBtn("btnEdit", false);
                    //EnabledLinkBtn("btnSave", true);
                    //EnabledLinkBtn("btnNotSave", false);
                    //EnabledLinkBtn("btnConfirm", false);
                    //EnabledLinkBtn("btnCancel", false);
                    this.ExecJScript("doEditGt('myGrid1');");
                    break;
                case "VIEW":
                    //EnabledLinkBtn("btnEdit", true);
                    //EnabledLinkBtn("btnSave", false);
                    //EnabledLinkBtn("btnNotSave", false);
                    //EnabledLinkBtn("btnConfirm", true);
                    //EnabledLinkBtn("btnCancel", true);
                    this.ExecJScript("doCancelGt('myGrid1');");
                    break;
            }
        }
        //获取数据原,"DP_DATA"字符一定不要改变，否则系统不能自动清除缓存
        public DataSet GetDisplayData(string strgtGridID, string strSrcF, string strsRadom)
        {
            if (strgtGridID == "myGrid1")
            {
                DataSet ds;
                if (strSrcF == "mFinder" || !this.SessionIsExists(strsRadom + "DP_DATA", "1"))//点击查询按钮时，或需要数据而没有缓存时，根据条件取出数据记录
                {
                    using (msBsc.AppLogic.DataAlc.CBizShpPromotionScoreLog objtem1 = new msBsc.AppLogic.DataAlc.CBizShpPromotionScoreLog())
                    {
                        if (this.GetParamHtValue("REL_ID") != string.Empty)
                        {
                            ds = objtem1.SelectByAccountNo(this.GetParamHtValue("REL_ID"));
                        }
                        else
                        {
                            ds = objtem1.DataObj.QueryBySQL("SELECT * FROM SHP_PROMOTION_SCORE_LOG WHERE (1=1)"
                                + (this.GetParamHtValue("ACCOUNT_NO") == string.Empty ? "" : " AND (ACCOUNT_NO='" + this.GetParamHtValue("ACCOUNT_NO") + "')")
                                + (this.GetParamHtValue("PUB_NO") == string.Empty ? "" : " AND (TRADING_NO='" + this.GetParamHtValue("PUB_NO") + "')")
                                + (this.GetParamHtValue("FWDUSER_NME") == string.Empty ? "" : " AND (FWDUSER_NME='" + this.GetParamHtValue("FWDUSER_NME") + "')")
                                + (this.GetParamHtValue("PROMOTION_NME") == string.Empty ? "" : " AND (PROMOTION_NME='" + this.GetParamHtValue("PROMOTION_NME") + "')")
                                + (this.GetParamHtValue("PAY_DIR") == string.Empty ? "" : " AND (PAY_DIR='" + this.GetParamHtValue("PAY_DIR") + "')") 
                                + "  ORDER BY INITIAL_DT ASC"); 
                        }
                    }
                    ds.AcceptChanges();
                    SetSession(strsRadom + "DP_DATA", ds, "1");
                    //查询时，将页面置为查看状态
                    //if (strSrcF == "mFinder")
                    SetMode("VIEW");
                }
                else
                {
                    ds = (DataSet)this.GetSession(strsRadom + "DP_DATA", "1");
                }
                return ds;
            }
            else
            {
                return null;
            }
        }
    }
}